package com.iplant.ams.controller.dms.maintenance;

import com.iplant.ams.service.DRMService;
import com.iplant.ams.service.po.dms.maintenance.DMSMaintenanceItemTask;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/DMSMaintenanceItemTask")
public class DMSMaintenanceItemTaskController extends BasicController<DMSMaintenanceItemTask> {
    private static Logger logger = LoggerFactory.getLogger(DMSMaintenanceItemTaskController.class);

    public DMSMaintenanceItemTaskController() {
        super(DMSMaintenanceItemTask.class);
    }

    @Autowired
    DRMService mDRMService;

    /**
     * 通过设备查询保养项目
     */
    @GetMapping("/QueryMaintenanceItemTaskByDevice")
    public Object QueryMaintenanceItemTaskByDevice(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            int wDeviceID = StringUtils.parseInt(request.getParameter("DeviceID"));
            int wConfigurationID = StringUtils.parseInt(request.getParameter("ConfigurationID"));
            String wName = StringUtils.parseString(request.getParameter("Name"));
            int wStatus = StringUtils.parseInt(request.getParameter("Status"));
            int wMaintenanceLevel = StringUtils.parseInt(request.getParameter("MaintenanceLevel"));
            int wType = StringUtils.parseInt(request.getParameter("Type"), 1);

            Pagination wPagination = GetPagination(request);

            ServiceResult<List<DMSMaintenanceItemTask>> wServiceResult =
                    mDRMService.DMS_QueryMaintenanceItemTaskByDevice(wLoginUser, wDeviceID, wConfigurationID, wName,
                            wStatus, wType, wMaintenanceLevel, wPagination);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 触发任务
     */
    @PostMapping("/TriggerTask")
    public Object TriggerTask(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            List<DMSMaintenanceItemTask> wDMSMaintenanceItemTaskList = CloneTool.CloneArray(wParam.get("data"),
                    DMSMaintenanceItemTask.class);

            int wType = StringUtils.parseInt(wParam.get("Type"), 1);

            if (wDMSMaintenanceItemTaskList == null || wDMSMaintenanceItemTaskList.size() == 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mDRMService.DMS_TriggerTaskMaintenanceItem(wLoginUser,
                    wDMSMaintenanceItemTaskList, wType);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 提交
     */
    @PostMapping("/Submit")
    public Object Submit(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            DMSMaintenanceItemTask wDMSMaintenanceItemTask = CloneTool.Clone(wParam.get("data"),
                    DMSMaintenanceItemTask.class);
            int wType = StringUtils.parseInt(wParam.get("Type"));
            if (wDMSMaintenanceItemTask == null || wDMSMaintenanceItemTask.ID <= 0 || wType <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mDRMService.DMS_SubmitMaintenanceItemTask(wLoginUser,
                    wDMSMaintenanceItemTask, wType);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 任务重置（全部已完成可重置，再次执行）
     */
    @GetMapping("/ResetTask")
    public Object ResetTask(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            int wDeviceID = StringUtils.parseInt(request.getParameter("DeviceID"));
            int wMaintenanceLevel = StringUtils.parseInt(request.getParameter("MaintenanceLevel"));
            if (wDeviceID <= 0 || wMaintenanceLevel <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mDRMService.DMS_ResetTask(wLoginUser, wDeviceID, wMaintenanceLevel);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }
}
